package com.zzx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzx.common.exception.ServiceException;
import com.zzx.entity.TProductOrderProduct;
import com.zzx.mapper.TProductOrderProductMapper;
import com.zzx.service.TProductOrderProductService;
import com.zzx.utils.EntityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zzx
 * @description 针对表【t_product_order_product】的数据库操作Service实现
 * @createDate 2024-11-30 17:14:43
 */
@Slf4j
@Service
public class TProductOrderProductServiceImpl extends ServiceImpl<TProductOrderProductMapper, TProductOrderProduct>
        implements TProductOrderProductService {
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(TProductOrderProduct param) {
        if (ObjectUtils.isEmpty(param.getProductOrderProductId())) {
            param.setProductOrderProductId(IdUtil.getSnowflakeNextId());
        } else {
            EntityUtils.mergeNonNullProperties(this.getBaseMapper().selectById(param.getProductOrderProductId()), param);
        }
        this.saveOrUpdate(param);
    }

    @Override
    public void addList(List<TProductOrderProduct> param) {
        param.forEach(e -> {
            if (ObjectUtils.isEmpty(e.getProductOrderProductId())) {
                e.setProductOrderProductId(IdUtil.getSnowflakeNextId());
            }
        });
        this.saveOrUpdateBatch(param);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteById(TProductOrderProduct param) {

        if (!ObjectUtils.isEmpty(param.getIds())) {
            this.removeBatchByIds(param.getIds());
        }
        if (!ObjectUtils.isEmpty(param.getProductOrderId())) {
            this.removeById(param.getProductOrderId());
        }
        if (!ObjectUtils.isEmpty(param.getProductOrderProductId())) {
            this.removeById(param.getProductOrderProductId());
        }
    }

    @Override
    public List<TProductOrderProduct> selectBy(TProductOrderProduct param) {
        LambdaQueryWrapper<TProductOrderProduct> wrapper = new LambdaQueryWrapper<>(TProductOrderProduct.class);
        if (!ObjectUtils.isEmpty(param.getProductOrderId())) {
            wrapper.eq(TProductOrderProduct::getProductOrderId, param.getProductOrderId());
        }
        // 多租户拦截器会自动添加 company_id 条件
        try {
            return this.getBaseMapper().selectList(wrapper);
        } catch (Exception e) {
            log.error("查询订单产品失败", e);
            throw new ServiceException("查询订单产品失败: " + e.getMessage());
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateStaus(TProductOrderProduct param) {

        LambdaQueryWrapper<TProductOrderProduct> wrapper = new LambdaQueryWrapper<>(TProductOrderProduct.class);
        if (!ObjectUtils.isEmpty(param.getProductOrderId())) {
            wrapper.eq(TProductOrderProduct::getProductOrderId, param.getProductOrderId());
        }
        if (!ObjectUtils.isEmpty(param.getProductOrderProductId())) {
            wrapper.eq(TProductOrderProduct::getProductOrderProductId, param.getProductOrderProductId());
        }
        // 多租户拦截器会自动添加 company_id 条件
        this.getBaseMapper().update(TProductOrderProduct.builder().status(param.getStatus()).build(), wrapper);
    }
}




